package com.niiwoo.civet.account.service.dubbo.check;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.AccountCheckingCheck;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingCheckMapperExt;
import com.niiwoo.civet.account.dto.request.AccountCheckingRecheckRequestDTO;
import com.niiwoo.civet.account.dto.response.AccountCheckingResponse;
import com.niiwoo.civet.account.service.check.AccountCheckingJobDubboService;
import com.niiwoo.civet.account.service.local.check.AccountCheckingJobService;
import com.niiwoo.civet.account.service.local.check.job.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service(version = "1.0.0", validation = "true")
public class AccountCheckingJobDubboServiceImpl implements AccountCheckingJobDubboService {

	@Autowired
	private AccountCheckingJobService accountCheckingJobService;
	@Autowired
	private AccountCheckingCheckMapperExt accountCheckingCheckMapperExt;

	@Override
	public void startAccountCheckingJob(String fileDate) {
		// 如果对账日期为空则取昨天为对账日期
		if (fileDate == null || "".equals(fileDate)) {
			fileDate = DateUtil.formatCheckfileDate();
		}

		accountCheckingJobService.startJob(fileDate);
	}

	@Override
	public void acountCheckingRecheckJob(AccountCheckingRecheckRequestDTO dto) {
		accountCheckingJobService.recheckJob(dto);
	}

	@Override
	public AccountCheckingResponse accountCompensationBillDate(String fileDate) {
		AccountCheckingResponse accountCheckingCheck = new AccountCheckingResponse();

		List<AccountCheckingCheck> list = accountCheckingCheckMapperExt.selectByBillDate(fileDate);
		if (list == null || list.isEmpty()){
			accountCheckingJobService.startJob(fileDate);
			accountCheckingCheck.setStatus(true);
			accountCheckingCheck.setCode((byte) 1);
			accountCheckingCheck.setFileDate(fileDate);
		}else{
			accountCheckingCheck.setStatus(true);
			accountCheckingCheck.setCode((byte) 0);
			accountCheckingCheck.setFileDate(fileDate);
			accountCheckingCheck.setErrorMessage("该账期已存在，无须再次生成");
		}
		return accountCheckingCheck;
	}


}
